package com.wgts.wgts.mvp.view.activity.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseFragment;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerSearchResultFragmentComponent;
import com.wgts.wgts.dagger.module.SearchResultFragmentModule;
import com.wgts.wgts.mvp.presenter.ISearchResultFragmentPresenter;
import com.wgts.wgts.mvp.view.ISearchResultFragmentView;
import com.wgts.wgts.mvp.view.activity.goodsinfo.JDGoodinfoActivity;
import com.wgts.wgts.netmodle.resp.SearchShopResultEntity;
import com.wgts.wgts.netmodle.resp.SearchStoreResultEntity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class SearchResultGoodsFragment extends BaseFragment implements ISearchResultFragmentView {
    @BindView(R.id.search_result_recycle_view)
    RecyclerView searchResultRecycleView;
    Unbinder unbinder;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.linear_empty)
    LinearLayout linearEmpty;
    @BindView(R.id.none_data_ly)
    LinearLayout noneDataLy;
    @BindView(R.id.net_error_ly)
    LinearLayout netErrorLy;

    private String name;

    private SearchResultGoodsAdapter searchResultGoodsAdapter;

    private List<SearchShopResultEntity.ShopList> shopLists;

    @Inject
    ISearchResultFragmentPresenter presenter;

    @Override
    public void init() {
        DaggerSearchResultFragmentComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .searchResultFragmentModule(new SearchResultFragmentModule(this))
                .build()
                .inject(this);
        shopLists = new ArrayList<>();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.search_result_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        Bundle bundle = getArguments();
        name = bundle.getString("name");
        initView();
        presenter.getShopList(name);
        return view;
    }


    private void initView() {
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadmore(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        searchResultRecycleView.setLayoutManager(linearLayoutManager);
        searchResultGoodsAdapter = new SearchResultGoodsAdapter(shopLists, getContext());
        searchResultRecycleView.setAdapter(searchResultGoodsAdapter);
        searchResultGoodsAdapter.setOnItemClickListern(new SearchResultGoodsAdapter.onItemClickListern() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), JDGoodinfoActivity.class);
                intent.putExtra(JDGoodinfoActivity.JOIN_TYPE, JDGoodinfoActivity.NOMAL);
                intent.putExtra("id", String.valueOf(shopLists.get(position).productId));
                startActivity(intent);
            }
        });
        load();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    public static Fragment getInstance(Bundle bundle) {
        Fragment fragment = new SearchResultGoodsFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onError(int status, String msg) {
        noneDataLy.setVisibility(View.GONE);
        netErrorLy.setVisibility(View.VISIBLE);
    }

    @Override
    public void getShopList(SearchShopResultEntity resultEntity, boolean noMore) {
        if (resultEntity.list.size() > 0){
            netErrorLy.setVisibility(View.GONE);
            noneDataLy.setVisibility(View.GONE);
        }else {
            netErrorLy.setVisibility(View.GONE);
            noneDataLy.setVisibility(View.VISIBLE);
        }
        shopLists.clear();
        shopLists.addAll(resultEntity.list);
        searchResultGoodsAdapter.notifyDataSetChanged();
        searchResultGoodsAdapter.setNoMore(noMore);
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
    }

    @Override
    public void loadMoreShop(SearchShopResultEntity resultEntity, boolean noMore) {
        refreshLayout.finishLoadmore();
        shopLists.addAll(resultEntity.list);
        searchResultGoodsAdapter.notifyDataSetChanged();
        searchResultGoodsAdapter.setNoMore(noMore);
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
    }

    @Override
    public void getStoreList(SearchStoreResultEntity resultEntity, boolean noMore) {

    }

    @Override
    public void loadMoreStore(SearchStoreResultEntity resultEntity, boolean noMore) {

    }

    private void load() {
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.getShopMore(name);
            }
        });
    }

    @OnClick(R.id.net_error_ly)
    public void onViewClicked() {
        presenter.getShopList(name);
    }
}
